import request from "@/utils/request";
import {QueueScreenModel} from "@/model/QueueScreenModel";

/**
 * 设备相关API
 */
export default class ScreenApi {

    /**
     * 查询当前系统大屏列表
     */
    public static listScreen(searchKey: string = null): Promise<QueueScreenModel[]> {
        return new Promise((resolve, reject) => {
            request({
                url: '/broadcast/screen/list',
                method: 'get',
                params: {
                   searchKey
                }
            }).then((res: any) => {
                if (res.code === 200) {
                    res.data = res.data.map(item => new QueueScreenModel().deserialize(item))
                    resolve(res.data)
                }
                reject(res);
            })
        })
    }

    /**
     * 创建大屏
     * @param screen
     */
    public static saveScreen(screen: QueueScreenModel): Promise<QueueScreenModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/screen/save`,
                method: 'POST',
                data: screen
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueScreenModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    /**
     * 更新大屏
     * @param id
     * @param screen
     */
    public static updateScreen(id:string, screen: QueueScreenModel): Promise<QueueScreenModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/broadcast/screen/${id}`,
                method: 'PUT',
                data: screen
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new QueueScreenModel().deserialize(res.data))
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

    public static deleteScreen(id:string): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            request({
                url: `/broadcast/screen/${id}`,
                method: 'DELETE',
            }).then((res: any) => {
                if (res) {
                    resolve(res)
                } else {
                    reject(res);
                }
            }, () => reject())
        })
    }

}

